package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.jaina.utils.HouseholdDomainConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.householdproperty.HouseholdPropertyDTO;
import com.guldan.jaina.dto.householdproperty.HouseholdPropertySearchDTO;
import com.guldan.mapper.db.entity.HouseholdProperty;
import com.guldan.mapper.db.entity.HouseholdPropertyExample;
import com.guldan.mapper.db.mapper.HouseholdPropertyMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class HouseholdPropertyManager extends BaseManager {

    @Autowired
    private HouseholdPropertyMapperExt householdPropertyMapperExt;

    //获取单个家庭属性信息
    public HouseholdPropertyDTO get(Integer householdPropertyId) {
        return HouseholdDomainConvertUtils.convert(householdPropertyMapperExt.selectByPrimaryKey(householdPropertyId));
    }

    //获取单个家庭属性信息，通过householdId
    public HouseholdPropertyDTO getByHouseholdId(Integer householdId) {
        HouseholdPropertyExample example = new HouseholdPropertyExample();
        HouseholdPropertyExample.Criteria criteria = example.createCriteria();
        criteria.andHouseholdIdEqualTo(householdId);
        criteria.andIsDeleteEqualTo(false);
        List<HouseholdProperty> householdPropertyList = householdPropertyMapperExt.selectByExample(example);
        HouseholdPropertyDTO dto = new HouseholdPropertyDTO();
        if (householdPropertyList != null && householdPropertyList.size() > 0) {
            dto = HouseholdDomainConvertUtils.convert(householdPropertyList.get(0));
        }
        return dto;
    }


    //新增/更新单个家庭属性，传入：HouseholdPropertyDTO
    public HouseholdPropertyDTO save(HouseholdPropertyDTO dto) {
        HouseholdProperty entity;
        if (dto.getHouseholdPropertyId() != null && dto.getHouseholdPropertyId() > 0) {
            entity = householdPropertyMapperExt.selectByPrimaryKey(dto.getHouseholdPropertyId());

            //entity.setHouseholdPropertyId(dto.getHouseholdPropertyId());
            entity.setIsLow(dto.getLow());
            entity.setLowAmount(dto.getLowAmount());
            entity.setIsFive(dto.getFive());
            entity.setFiveTypeCode(dto.getFiveTypeCode());
            entity.setIsDifficult(dto.getDifficult());
            entity.setIsDestitute(dto.getDestitute());
            entity.setIsPlains(dto.getPlains());
            entity.setIsSjyDifficult(dto.getSjyDifficult());
            entity.setIsSjyFuel(dto.getSjyFuel());
            entity.setIsForestEcology(dto.getForestEcology());
            entity.setApplyEcologySum(dto.getApplyEcologySum());
            entity.setEcologyRemark(dto.getEcologyRemark());
            entity.setHouseholdId(dto.getHouseholdId());


            entity.setModifyTime(DateUtil.getCurrentTime());
//            entity.setModifier(dto.getModifier());
            householdPropertyMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new HouseholdProperty();

            entity.setIsLow(dto.getLow());
            entity.setLowAmount(dto.getLowAmount());
            entity.setIsFive(dto.getFive());
            entity.setFiveTypeCode(dto.getFiveTypeCode());
            entity.setIsDifficult(dto.getDifficult());
            entity.setIsDestitute(dto.getDestitute());
            entity.setIsPlains(dto.getPlains());
            entity.setIsSjyDifficult(dto.getSjyDifficult());
            entity.setIsSjyFuel(dto.getSjyFuel());
            entity.setIsForestEcology(dto.getForestEcology());
            entity.setApplyEcologySum(dto.getApplyEcologySum());
            entity.setEcologyRemark(dto.getEcologyRemark());
            entity.setHouseholdId(dto.getHouseholdId());

//            entity.setDisabled(dto.getDisabled());
//            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            householdPropertyMapperExt.insertSelective(entity);
        }
        dto = HouseholdDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个家庭属性信息
    public Boolean delete(Integer householdPropertyId) {
        HouseholdProperty entity = householdPropertyMapperExt.selectByPrimaryKey(householdPropertyId);
        entity.setIsDelete(true);
        return householdPropertyMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }

    //分页查询家庭属性信息列表
    public Pagination<HouseholdPropertyDTO> list(HouseholdPropertySearchDTO householdPropertySearchDTO) {
        /*Pagination<HouseholdPropertyDTO> result = new Pagination<HouseholdPropertyDTO>(householdPropertySearchDTO.getCurrentPage(), householdPropertySearchDTO.getPerPage());
        HouseholdPropertyExample example = buildSearchExample(householdPropertySearchDTO);
        int total = householdPropertyMapperExt.countByExample(example);
        result.setTotalCount(total);
        if (total > 0) {
            List<HouseholdPropertyDTO> data = new ArrayList<HouseholdPropertyDTO>();
            List<HouseholdProperty> list = householdPropertyMapperExt.selectByExampleWithPaging(example, result.getOffset(), result.getPageSize());
            for (HouseholdProperty entity : list) {
                HouseholdPropertyDTO dto = HouseholdDomainConvertUtils.convert(entity);
                data.add(dto);
            }
            result.setList(data);
        }
        return result;*/
        return null;
    }

    //build查询Example
    private HouseholdPropertyExample buildSearchExample(HouseholdPropertySearchDTO search) {
        HouseholdPropertyExample example = new HouseholdPropertyExample();
        HouseholdPropertyExample.Criteria criteria = example.createCriteria();

        if (search.getHouseholdPropertyId() != null && search.getHouseholdPropertyId() > 0) {
            criteria.andHouseholdPropertyIdEqualTo(search.getHouseholdPropertyId());
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("household_property_id asc");
        }
        return example;
    }

}
